//同步请求的工具类
var interface_config = require('../interfaceConfig.js')
var Promise = require('../plugins/es6-promise.js')
var app = getApp()
/**
 * 
 *  es6-工具类
 * 
*/

export const promistUtils = (fn) => {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
     
        resolve(res)
      }
      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}


Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  );
};

//发送特殊的post请求
function postlgRequest(data, url, token) {
  var postRequest = promistUtils(wx.request)
  return postRequest({
    url: interface_config.host + url,
    method: 'POST',
    data: data,
    header: {
      "content-type": "application/x-www-form-urlencoded",
      "auth": token
    },
    dataType: 'json',

  })
}

//发送特殊的get请求
function getlgRequest(data, url,token) {
  var postRequest = promistUtils(wx.request)
  return postRequest({
    url: interface_config.host + url,
    method: 'GET',
    data: data,
    header: {
      "content-type": "application/x-www-form-urlencoded",
      "auth":token
    },
    dataType: 'json',

  })
}

//发送请求get
function getRequest(data, url) {
  var postRequest = promistUtils(wx.request)
  return postRequest({
    url: interface_config.host + url,
    method: 'GET',
    data: data,
    header: {
      "content-type": "application/x-www-form-urlencoded"
    },
    dataType: 'json',

  })
}

// 发送请求post
function postRequest(data,url) {
  var postRequest = promistUtils(wx.request)
  return postRequest({
    url: interface_config.host+url,
    method: 'POST',
    data: data,
    header: {
      "content-type": "application/x-www-form-urlencoded"
    },
    dataType: 'json',

  })
}




module.exports = {
  postRequest:postRequest,
  getRequest:getRequest,
  getlgRequest:getlgRequest,
  postlgRequest:postlgRequest
}


